package com.study.exception;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@ControllerAdvice
@Slf4j
public class GlobalException {
    @ExceptionHandler(AuthorizationException.class)
    public ModelAndView handlerException(Exception e, HttpServletRequest request, HttpSession session) {

        ModelAndView modelAndView = new ModelAndView();
        //指定错误页面的模板页
        modelAndView.setViewName("error");

        request.setAttribute("code", -100);
        request.setAttribute("message", "你没有权限操作");

        //装到session
        session.setAttribute("sessionName", "小明");
        ServletContext servletContext = request.getServletContext();
        //装到ServletContext
        servletContext.setAttribute("applicationNum", 1);
        return modelAndView;
    }
}

